import * as echarts from 'echarts'

export const initCharts = (refs: Record<string, HTMLElement | null>) => {
  // 确保所有图表容器都存在
  if (!refs.pollutionChart || !refs.trendChart || !refs.compositionChart) {
    console.warn('部分图表容器未找到')
    // 继续执行，初始化可用的图表
  }

  // 设置容器尺寸
  const setContainerSize = (container: HTMLElement) => {
    if (container.offsetHeight < 50) {
      container.style.height = '140px'
    }
  }

  // 污染物统计图
  if (refs.pollutionChart) {
    setContainerSize(refs.pollutionChart)
    // 如果已经有实例，先销毁
    const existingChart = (refs.pollutionChart as any)._echarts_instance_
    if (existingChart) {
      existingChart.dispose()
    }
    const pollutionChart = echarts.init(refs.pollutionChart)
    pollutionChart.setOption({
      grid: {
        top: 20,
        right: 20,
        bottom: 30,
        left: 50,
        containLabel: true
      },
      xAxis: {
        type: 'category',
        data: ['00:00', '03:00', '06:00', '09:00', '12:00', '15:00', '18:00', '21:00'],
        axisLine: { lineStyle: { color: '#0ff' } },
        axisLabel: { fontSize: 10, color: '#fff' }
      },
      yAxis: {
        type: 'value',
        axisLine: { lineStyle: { color: '#0ff' } },
        axisLabel: { fontSize: 10, color: '#fff' },
        splitLine: { lineStyle: { color: 'rgba(0,255,255,0.1)' } }
      },
      series: [{
        data: [120, 200, 150, 80, 70, 110, 130, 90],
        type: 'bar',
        itemStyle: { 
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: '#0ff' },
            { offset: 1, color: 'rgba(0,255,255,0.3)' }
          ])
        },
        barWidth: '60%'
      }]
    })
    pollutionChart.resize()
  }

  // 指标变化图
  if (refs.indexChart) {
    setContainerSize(refs.indexChart)
    const existingChart = (refs.indexChart as any)._echarts_instance_
    if (existingChart) {
      existingChart.dispose()
    }
    const indexChart = echarts.init(refs.indexChart)
    indexChart.setOption({
      grid: {
        top: 30,
        right: 20,
        bottom: 30,
        left: 50
      },
      xAxis: {
        type: 'category',
        data: ['AQI', 'PM2.5', 'PM10', 'SO2', 'NO2', 'CO'],
        axisLine: { lineStyle: { color: '#0ff' } }
      },
      yAxis: {
        type: 'value',
        axisLine: { lineStyle: { color: '#0ff' } }
      },
      series: [{
        data: [120, 200, 150, 80, 70, 110],
        type: 'line',
        smooth: true,
        itemStyle: { color: '#0ff' },
        lineStyle: { color: '#0ff' }
      }]
    })
    indexChart.resize()
  }

  // 监测趋势图
  if (refs.trendChart) {
    setContainerSize(refs.trendChart)
    const existingChart = (refs.trendChart as any)._echarts_instance_
    if (existingChart) {
      existingChart.dispose()
    }
    const trendChart = echarts.init(refs.trendChart)
    trendChart.setOption({
      grid: {
        top: 30,
        right: 20,
        bottom: 30,
        left: 50
      },
      xAxis: {
        type: 'category',
        data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
        axisLine: { lineStyle: { color: '#0ff' } }
      },
      yAxis: {
        type: 'value',
        axisLine: { lineStyle: { color: '#0ff' } }
      },
      series: [{
        data: [820, 932, 901, 934, 1290, 1330, 1320],
        type: 'line',
        smooth: true,
        itemStyle: { color: '#0ff' },
        lineStyle: { color: '#0ff' },
        areaStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
              offset: 0,
              color: 'rgba(0,255,255,0.3)'
            }, {
              offset: 1,
              color: 'rgba(0,255,255,0)'
            }]
          }
        }
      }]
    })
    trendChart.resize()
  }

  // 构成分析饼图
  if (refs.compositionChart) {
    setContainerSize(refs.compositionChart)
    const existingChart = (refs.compositionChart as any)._echarts_instance_
    if (existingChart) {
      existingChart.dispose()
    }
    const compositionChart = echarts.init(refs.compositionChart)
    compositionChart.setOption({
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'vertical',
        right: 10,
        top: 'center',
        textStyle: { color: '#fff' }
      },
      series: [{
        type: 'pie',
        radius: ['40%', '70%'],
        center: ['40%', '50%'],
        data: [
          { value: 28.7, name: '工业污染', itemStyle: { color: '#ff7b7b' } },
          { value: 25.8, name: '生活污染', itemStyle: { color: '#7b7bff' } },
          { value: 24.7, name: '农业污染', itemStyle: { color: '#7bff7b' } },
          { value: 20.8, name: '其他污染', itemStyle: { color: '#ffff7b' } }
        ],
        label: {
          color: '#fff'
        }
      }]
    })
    compositionChart.resize()
  }
} 